package com.stylefeng.guns.modular.customer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.modular.customer.dao.CustomerCompanyDao;
import com.stylefeng.guns.modular.customer.dao.PurchaseDao;
import com.stylefeng.guns.modular.customer.service.IPurchaseService;
import com.stylefeng.guns.modular.customer.transfer.PurchaseXlsM;
import com.stylefeng.guns.modular.system.dao.PurchaseMapper;
import com.stylefeng.guns.modular.system.model.CustomerCompany;
import com.stylefeng.guns.modular.system.model.PayPlan;
import com.stylefeng.guns.modular.system.model.Purchase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.ValidationException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采购配套 服务实现类
 * </p>
 *
 * @author yinjc123
 * @since 2018-04-03
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements IPurchaseService {

    @Autowired
    private PurchaseDao purchaseDao;
    @Autowired
    private CustomerCompanyDao customerCompanyDao;

    @Override
    public List<Map<String, Object>> selectPurchasePage(Page<Purchase> page,Integer customerCompanyId, String customerCompanyName) {
        return purchaseDao.getPurchasePage(page, customerCompanyId, customerCompanyName);
    }
    @Transactional
    @Override
    public void insertXlsListPurchase(List<PurchaseXlsM> xlsMList) {
        for (PurchaseXlsM xlsM : xlsMList) {
            CustomerCompany customerCompany = customerCompanyDao.selectByUnifiedSocialCreditCode(xlsM.getUnifiedSocialCreditCode());
            if (customerCompany == null) {
                throw new ValidationException(xlsM.getCustomerCompanyName()+"不存在,统一社会信用代码为:" +  xlsM.getUnifiedSocialCreditCode());
            }
            xlsM.setCustomerCompanyId(customerCompany.getId());
        }
        for (PurchaseXlsM xlsM : xlsMList) {
            Purchase purchase = new Purchase();
            BeanKit.copyProperties(xlsM,purchase);
            purchase.setAnnualPurchaseAmount(strChangeBigDecimal(xlsM.getAnnualPurchaseAmount()));
            purchase.setAccountPayableBalance(strChangeBigDecimal(xlsM.getAccountPayableBalance()));
            purchase.setCashPaid(strChangeBigDecimal(xlsM.getCashPaid()));
            purchase.setSilverTicketPaid(strChangeBigDecimal(xlsM.getSilverTicketPaid()));
            purchase.setBusinessTicketPaid(strChangeBigDecimal(xlsM.getBusinessTicketPaid()));
            purchase.setBusinessTicketDiscount(strChangeBigDecimal(xlsM.getBusinessTicketDiscount()));
            purchase.setDiscountInterestRate(strChangeBigDecimal(xlsM.getDiscountInterestRate()));
            purchase.setProviderProductBalance(strChangeBigDecimal(xlsM.getProviderProductBalance()));
            super.insert(purchase);
        }
    }
        private BigDecimal strChangeBigDecimal(String str){
            if(str==null||str==""){
                return new BigDecimal(0);
            }
              return new BigDecimal(str);
        }


    @Override
    public void deleteByCustomerCompanyId(Integer customerCompanyId) {
        EntityWrapper<Purchase> ew = new EntityWrapper<Purchase>();
        ew.eq("customer_company_id", customerCompanyId);
        delete(ew);
    }
}
